package cn.iocoder.yudao.module.personal.job;

import cn.hutool.core.date.DateUtil;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.system.api.sms.SmsSendApi;
import cn.iocoder.yudao.module.system.api.sms.dto.send.SmsSendSingleToUserReqDTO;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: huqngqi
 * @date: 2024/12/2 17:33
 * @description: 天气预警
 */
@Component
@Slf4j
public class MemoReminderJob implements JobHandler {
    @Resource
    private SmsSendApi smsSendApi;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public String execute(String param) {
        if (StringUtils.isBlank(param)) {
            return "入参为空";
        }
        JSONObject paramJson = JSONObject.parseObject(param);
        String mobiles = paramJson.getString("mobile");
        if (StringUtils.isBlank(mobiles)) {
            return "手机号为空";
        }
        String templateCode = paramJson.getString("templateCode");
        Long channelId = paramJson.getLong("channelId");
        String content = paramJson.getString("content");
        String weatherWarningToday = stringRedisTemplate.opsForValue().get("weather_warning_today");
        if(StringUtils.isBlank(weatherWarningToday)){
            weatherWarningToday = "0℃";
        }
        content = weatherWarningToday + "," + content;
        Map<String, Object> templateParams = new HashMap<>();
        templateParams.put("todayStr", DateUtil.formatDate(new Date()));
        templateParams.put("qwToday", content);
        String[] mobilesAttr = mobiles.split(",");
        for (String mobile : mobilesAttr) {
            SmsSendSingleToUserReqDTO reqDTO = new SmsSendSingleToUserReqDTO();
            reqDTO.setMobile(mobile);
            reqDTO.setTemplateCode(templateCode);
            reqDTO.setTemplateParams(templateParams);
            reqDTO.setChannelId(channelId);
            smsSendApi.sendSingleSmsDirectToMobile(reqDTO);
        }
        return "执行成功";
    }
}
